/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.home2;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.isisufes.R;
//import android.isisufes.services.FileManagerDaemon;
import android.isisufes.services.PollingDaemon;
import android.isisufes.util.DBHelper;
import android.isisufes.util.ServerCom;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Home2 extends Activity {
	
	private DBHelper database;
	
	private SQLiteDatabase myBase;
	
	private String userName, passWord;
	private String fullName = null;
	
	private Intent polling = null;
	//private Intent fileManagerDemo;
	
	public boolean contCheck = false;
	
	int previousScreen = 0;
	
	static final int NONE = 0;
	static final int HOME = 1;
	static final int COMMU = 2;
//	static final int CAMP = 3;  // Não é mais usado
	static final int CONT = 4;
	static final int FMANAGER = 5;
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode ) {
			if (previousScreen == HOME ) {
				setTitle("Join Us! - Home");
				this.setContentView(R.layout.home2);
				 setUpBut();
			} else if (previousScreen == COMMU ) {
				new ListViewFactory(Home2.this,myBase,2);
			} else if (previousScreen == NONE ) {
				this.finish();
			} else /*if (previousScreen == CAMP ) {
				new ListViewFactory(Home2.this,myBase,1);
			} else*/ if (previousScreen == CONT ) {
				new ListViewFactory(Home2.this,myBase,3);
			} else if (previousScreen == FMANAGER ) {
				new ListViewFactory(Home2.this,myBase,5);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		
		// Get username and password from intent extras.
		Bundle b = this.getIntent().getExtras();
		this.userName = b.getString("un");
		this.passWord = b.getString("pw");
		
		//start services
//		fileManagerDemo = new Intent(Home2.this, FileManagerDaemon.class);
//		fileManagerDemo.putExtras(b);		
//		stopService(fileManagerDemo);
//		startService(fileManagerDemo);
		
		// Start polling service.
		if(polling == null){
			polling = new Intent(this, PollingDaemon.class);
			polling.putExtra("username", userName);
			polling.putExtra("password", passWord);
			startService(polling);
		}

		// load database
		database = new DBHelper();
		myBase = this.openOrCreateDatabase("homedb", MODE_PRIVATE, null);
		database.onCreate(myBase);	
	
		// loads the initial layout
		this.setContentView(R.layout.home2);
		this.getLayoutInflater().inflate(R.layout.home2, null);
		
		// setup the buttons
		setUpBut();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopService(polling);
		polling = null;
		myBase.close();
	}
	
	/**
	 * @param bundle Um bundle cujos mapeamentos devem ser copiados para o novo
	 * ou null.
	 * @return Um novo bundle contendo as chaves "username" e "password". 
	 */
	private Bundle getUserInfoBundle(Bundle bundle) {
		bundle = bundle != null ? new Bundle(bundle) : new Bundle();
		bundle.putString("username", this.userName);
		bundle.putString("password", this.passWord);
		bundle.putString("fullname", getFullUserName());
		return bundle;
	}
	
	private void setUpBut() {
		this.previousScreen = NONE;
		
		/* Menu buttons */
		
		// Invitations.
		ImageButton b1 = (ImageButton) findViewById(R.id.home2_invitations);
		b1.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				new ListViewFactory(Home2.this,myBase,0);
			}
		});
		
		// Campaigns button.
		ImageButton b2 = (ImageButton) findViewById(R.id.home2_campaigns);
		b2.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Log.d("Home2", "Starting campaigns activity.");
				Intent i = new Intent(getApplicationContext(), CampaignListView.class);
				i.putExtras(getUserInfoBundle(null));
				startActivity(i);
				
//				new ListViewFactory(Home2.this,myBase,1);  // Visualização antiga
			}
		});
		
		// Communities button.
		ImageButton b3 = (ImageButton) findViewById(R.id.home2_communities);
		b3.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				new ListViewFactory(Home2.this,myBase,2);
			}
		});
		
		// Contacts button.
		ImageButton b4 = (ImageButton) findViewById(R.id.home2_contacts);
		b4.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				new ListViewFactory(Home2.this, myBase, 3);
			}
		});
		
		// Profile button.
		ImageButton b5 = (ImageButton) findViewById(R.id.home2_profile);
		b5.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
//				Intent profileManager = new Intent(Home2.this, ProfileManager.class);
//				Bundle extra = new Bundle();
//				extra.putString("un", userName);
//				extra.putString("pw", passWord);
//				profileManager.putExtras(extra);
//				startActivity(profileManager);
				
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), SimpleProfileActivity.class);
				intent.putExtra("username", userName);
				intent.putExtra("password", passWord);
				startActivity(intent);
			}
		});
		
		// Messages button.
		ImageButton b6 = (ImageButton) findViewById(R.id.home2_msgs);
		b6.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				new ListViewFactory(Home2.this,myBase,4);
			}
		});
		
		// Files button.
		ImageButton b7 = (ImageButton) findViewById(R.id.home2_fm);
		b7.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				new ListViewFactory(Home2.this,myBase,5);
			}
		});
		
		// Help button.
		ImageButton b8 = (ImageButton) findViewById(R.id.home2_help);
		b8.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent help = new Intent(Intent.ACTION_WEB_SEARCH);
				help.setData(Uri.parse("http://200.137.64.30/help/"));
				startActivity(help);
			}
		});
	}

	public String getPassword() {
		return passWord;
	}

	public String getUsername() {
		return userName;
	}

	public long x_area, y_area, radius;
	
	public SQLiteDatabase getMyBase() {
		return myBase;
	}

	public String getFullUserName() {
		if (this.fullName == null) { 
			this.fullName = ServerCom.GetUserName(ServerCom.getId() + "");
		}
		return this.fullName;
	}
	
	/* Options menu */
	
	// Identificadores únicos para serem usados pelo callback do clique no menu.
	private static final int MENU_LOGOUT = 1;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, MENU_LOGOUT, Menu.NONE, "Logout");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_LOGOUT:
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}